home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / CDaemon / HACK16 / Sources / Hack 16 out.cp < prev    next >
Encoding:
Text File  |  2001-06-23  |  4.9 KB  |  237 lines

  1. /*
  2.  *  © 2001 Shawn Platkus.
  3.  *
  4.  *  Hack 16 for MacHack 16
  5.  *
  6.  *  Main source file for CDaemon
  7.  */
  8.  
  9. #include "Hack 16 out.h"
  10.  
  11. // Globals
  12. bool             gbQuit = false;
  13. const short        StackSize = 64;    
  14.  
  15. // Application constructor
  16. Application::Application()
  17. {
  18.     Initialize();
  19. }
  20.  
  21. void Application::Initialize()
  22. {
  23. #ifdef DEVELOPMENT    // for debugging
  24. #if !TARGET_API_MAC_CARBON
  25.     InitGraf(&qd.thePort);
  26.     InitFonts();
  27.     InitWindows();
  28.     InitMenus();
  29.     TEInit();
  30.     InitDialogs(nil);
  31. #endif
  32.     InitCursor();
  33. #endif
  34. }
  35.  
  36. void Application::RunTest()
  37. {
  38. #ifdef DEVELOPMENT    // for debugging
  39.     char            theInputChar;
  40. #endif
  41.     OSErr            status = noErr;
  42.     UInt32            theDeviceNumber;
  43.     SInt16            drvrRefNum  = 0;
  44.  
  45. #ifdef DEVELOPMENT    // for debugging
  46.     SIOUXSettings.asktosaveonclose = false;        // Metrowerks only
  47. #endif
  48.  
  49. #ifdef DEVELOPMENT    // for debugging
  50.     // Intro
  51.     printf ("Hello,\nI'm going to try to detect your CD-ROM drive and display some\ninformation about it.\nPress return to continue.\n");
  52.     theInputChar = getchar();
  53.     printf ("OK, here we go...\n\n\n");
  54. #endif
  55.  
  56.     // Check for ATA Hardware 
  57.     // you should do this before calling the ATAManager, since some early ROMS
  58.     // could indicate an ATA manager without the proper HW, thus casing a crash.
  59.     if (ATAHardwarePresent() == false)
  60.     {
  61.         SysBeep(33);
  62. #ifdef DEVELOPMENT    // for debugging
  63.         printf("ATA Hardware is not present on this system\n");
  64.         exit(EXIT_FAILURE);
  65. #endif
  66.     }
  67.     else
  68.     {
  69.         // Check for ATA Manager
  70.         if (ATAManagerPresent() == false)
  71.         {
  72.             SysBeep(33);
  73. #ifdef DEVELOPMENT    // for debugging
  74.             printf("ATA Manager is not present on this system\n");
  75.             exit(EXIT_FAILURE);
  76. #endif
  77.         }
  78.         else
  79.         {
  80.             // Display ATA Manager Info
  81. #ifdef DEVELOPMENT    // for debugging
  82.             status = DisplayATAManagerInquiryInfo();
  83. #endif
  84.             if (status != noErr)
  85.             {
  86.             SysBeep(33);
  87. #ifdef DEVELOPMENT    // for debugging
  88.                 printf("Cannot access ATA Manager: %d\n", (int) status);
  89.                 exit(EXIT_FAILURE);
  90. #endif
  91.             }
  92.             else
  93.             {
  94.                 // Disable the CD driver 
  95.                 status = OpenDriver("\p.AppleCD", &drvrRefNum);    
  96.                 if (status == noErr)
  97.                 {
  98. #ifdef DEVELOPMENT    // for debugging
  99.                     printf("Found .AppleCD driver, preparing to disable it\n");
  100.                     status = DisableCDDriver(drvrRefNum);
  101. #endif
  102.                  }
  103.                 
  104.                 // Display ATA Device Info
  105.                 theDeviceNumber = ScanATABusses();
  106.                 if (theDeviceNumber == -1)
  107.                 {
  108. #ifdef DEVELOPMENT    // for debugging
  109.                     printf("No CD ROM Drive Found or ATA Manager Error: %d\n", (int) theDeviceNumber);
  110.                     //exit(EXIT_FAILURE);
  111. #endif
  112.                 }
  113.                 else
  114.                 {
  115.                     // Try to eject the CD Drive.
  116. #ifdef DEVELOPMENT    // for debugging
  117.                     printf ("OK, I'm going to try to eject your CD ROM Drive at device #%i\n", theDeviceNumber);
  118.                     printf ("Press Return To Continue...\n");
  119.                     theInputChar = getchar();
  120. #endif
  121.                     status = SetupATAPI(theDeviceNumber);
  122.                     if (status == noErr)
  123.                     {
  124.                         status = xJect(true);
  125.                     }
  126. #ifdef DEVELOPMENT    // for debugging
  127.                     printf (" The result that I got was %i\n", status);
  128.                     printf ("-=> ");
  129.                     theInputChar = getchar();
  130.  
  131.                     // Try to INject the CD Drive.
  132.                     printf ("OK, I'm going to try to INject your CD ROM Drive at device #%i\n", theDeviceNumber);
  133.                     printf ("Press Return To Continue...\n");
  134.                     theInputChar = getchar();
  135. #endif
  136.                     status = xJect(false);
  137.  
  138. #ifdef DEVELOPMENT    // for debugging
  139.                     printf ("Did the CD-Drive INject or close? (type yes or no and then return to continue)\n");
  140.                     printf (" The result that I got was %i\n", status);
  141.                     printf ("-=> ");
  142.                     theInputChar = getchar();
  143. #endif
  144.                 }
  145.  
  146.                 // Renable the CD driver
  147.                 if(drvrRefNum != 0)
  148.                 {
  149. #ifdef DEVELOPMENT    // for debugging
  150.                     printf("Re-enabling the .AppleCD driver \n");\
  151.                     printf("Execution complete... program ended\n\n");
  152. #endif
  153.                     status = EnableCDDriver(drvrRefNum);
  154.                 }
  155.             }
  156.         }
  157.     }    
  158. }
  159.  
  160.  
  161. void Application::IncreaseFBAStack(Size extraBytes)
  162. {
  163.     THz            myZone;
  164.     SysEnvRec    sys;
  165.  
  166.     SysEnvirons(1, &sys);
  167.  
  168.     if (sys.systemVersion < 0x0755)
  169.     {
  170.         // Check that we aren't running with a corrupt heap. If we are,
  171.         // fix the problem.  This was a bug with FBA's before System 7.5.5.  
  172.         myZone = GetZone();
  173.         if (myZone->bkLim != LMGetHeapEnd())
  174.             LMSetHeapEnd(myZone->bkLim);
  175.     }
  176.     // Increase the stack size by lowering the heap limit.
  177.     SetApplLimit((Ptr) ((Size) GetApplLimit() - extraBytes));
  178. }
  179.  
  180.  
  181. void Application::CheckEventQueue()
  182. {
  183.     EventRecord             anEvent;
  184.     OSErr                    err;
  185.  
  186.     WaitNextEvent(highLevelEventMask, &anEvent, 120, NULL);
  187.     
  188.     switch (anEvent.what)
  189.     {
  190.         case kHighLevelEvent :
  191.             err = DoAppleEvent(&anEvent);
  192.         break;
  193.     }
  194.     
  195.     xJect(false);
  196. }
  197.  
  198.  
  199. void Application::Run()
  200. {
  201.  
  202.     OSErr    status;
  203.     IncreaseFBAStack(StackSize * 1024);
  204.  
  205.     MaxApplZone();
  206.     
  207.     status = InitAppleEvents();
  208.     if (status != noErr)
  209.     {
  210.         SysBeep(33);
  211. #ifdef DEVELOPMENT    // for debugging
  212.         printf("This program failed to initialize Apple events\n\n");
  213. #endif
  214.     }
  215.     else
  216.     {
  217.         RunTest();
  218.  
  219.         while ( ! gbQuit )
  220.         {
  221.             CheckEventQueue();
  222.             
  223.     #ifdef DEVELOPMENT    // for debugging
  224.             gbQuit = true;
  225.     #endif
  226.         }
  227.     }
  228. }
  229.  
  230.  
  231. void main(void)
  232. {
  233.     Application theApplication;
  234.     
  235.     theApplication.Run();
  236. }
  237.